/*
 * @(#)Test.java    Nov 17, 2010
 *
 * Copyright 2009 eBaoTech, Inc. All rights reserved.
 */

package cn.com.elite;

import java.io.IOException;
import java.io.StringReader;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.cntaiping.intserv.client.ISHttpClient;
import com.cntaiping.intserv.client.exception.WrongFormParameterException;

public class TesOnlinePayment {

	public static void main(String[] args) throws Exception {
	//	testPaymentQuery();
		testPayment();
//		testPaymentCancel();
	}

	private static void testPayment() throws Exception {
		ISHttpClient client = new ISHttpClient();
		String xml = "<?xml version='1.0' encoding='UTF-8'?><BODY>" +
				"<MERCHANT_ID>888119948140055</MERCHANT_ID>" +
				"<PAYMENT_ID>00008003279830955555</PAYMENT_ID>" +
				"<FEE_TYPE></FEE_TYPE>" +
				"<PAY_WAY></PAY_WAY>" +
				"<BUSI_TYPE></BUSI_TYPE>" +
				"<BANK_ACCOUNT>645454646464646</BANK_ACCOUNT>" +
				"<BANK_CODE></BANK_CODE>" +
				"<HOLDER_NAME>周琪瑶</HOLDER_NAME>" +
				"<HOLDER_CERTI_TYPE>1</HOLDER_CERTI_TYPE>" +
				"<HOLDER_CERTI_CODE>330302198506103624</HOLDER_CERTI_CODE>" +
				"<TRANS_AMT>300.00</TRANS_AMT>" +
				"<CUR_TYPE>RMB</CUR_TYPE>" +
				"<RESERVED1></RESERVED1>" +
				"<RESERVED2></RESERVED2>" +
				"</BODY>";
		client.connect("http://10.1.172.5:7001/intserv/servlet/com.cntaiping.intserv.insu.servlet.EBPPPaymentServlet", null, "300", "ftp123");
		client.addParameter("indentity", "300");
		client.addParameter("xmlfile", xml);
		String response = client.doPost(true);

		System.out.println(response);
	}

	private static void testPaymentCancel() throws Exception {
		ISHttpClient client = new ISHttpClient();
		String xml = "<?xml version='1.0' encoding='UTF-8'?><BODY><MERCHANT_ID>888119948140055</MERCHANT_ID><PAYMENT_ID>00008003279830900009</PAYMENT_ID><ORG_PAYMENT_ID>00008003279830900008</ORG_PAYMENT_ID></BODY>";
		client.connect("http://10.1.172.5:7001/intserv/servlet/com.cntaiping.intserv.insu.servlet.EBPPPaymentCancelServlet", null, "300", "ftp123");
		client.addParameter("indentity", "300");
		client.addParameter("xmlfile", xml);
		String response = client.doPost(true);

		print(response);
	}

	private static void testPaymentQuery() throws WrongFormParameterException {
		ISHttpClient client = new ISHttpClient();
		String xml = "<?xml version='1.0' encoding='UTF-8'?><BODY><MERCHANT_ID>888119948140055</MERCHANT_ID><PAYMENT_ID>00008003279830900009</PAYMENT_ID></BODY>";
		client.connect("http://10.1.172.5:7001/intserv/servlet/com.cntaiping.intserv.insu.servlet.EBPPPaymentQueryServlet", null, "300", "ftp123");
		client.addParameter("indentity", "300");
		client.addParameter("xmlfile", xml);

		long start = System.currentTimeMillis();
		String response = "";
		try {
			// 将网段切换到7, 模拟网络超时.
			response = client.doPost(true);
		} catch (IOException e) {
			long end = System.currentTimeMillis();
			System.out.println(end);
			System.out.println((end - start) / 1000 + "秒");

			System.out.println(e.getMessage());
		}

		print(response);
	}

	private static void print(String response) {
		System.out.println(response);

		Element root = null;
		try {
			root = ElementRoot(response);
		} catch (Exception e) {
			e.printStackTrace();

			return;
		}

		String orderNo = getElementValue(root.getChild("ORDER_NUMBER"));
		String responseCode = getElementValue(root.getChild("RESPONSE_CODE"));
		String responseDesc = getElementValue(root.getChild("RESPONSE_DESC"));
		String paymentId = getElementValue(root.getChild("PAYMENT_ID"));
		String transDate = getElementValue(root.getChild("TRANS_DATE"));
		String transTime = getElementValue(root.getChild("TRANS_TIME"));
		String feeType = getElementValue(root.getChild("FEE_TYPE"));
		String payWay = getElementValue(root.getChild("PAY_WAY"));
		String busiType = getElementValue(root.getChild("BUSI_TYPE"));
		String bankAccount = getElementValue(root.getChild("BANK_ACCOUNT"));
		String bankCode = getElementValue(root.getChild("BANK_CODE"));
		String accountType = getElementValue(root.getChild("ACCOUNT_TYPE"));
		String holderName = getElementValue(root.getChild("HOLDER_NAME"));
		String holderCertiType = getElementValue(root.getChild("HOLDER_CERTI_TYPE"));
		String holderCertiCode = getElementValue(root.getChild("HOLDER_CERTI_CODE"));
		String transAmt = getElementValue(root.getChild("TRANS_AMT"));
		String curType = getElementValue(root.getChild("CUR_TYPE"));
		String rsv1 = getElementValue(root.getChild("RESERVED1"));
		String rsv2 = getElementValue(root.getChild("RESERVED2"));
		String errMsg = getElementValue(root.getChild("ERR_MSG"));
		String errCode = getElementValue(root.getChild("ERR_CODE"));

		System.out.println("--------------------- OnlinePaymentBean.parsePacket 4 解析银联报文: ");
		System.out.println("-------------------------- 报文明细_ORDER_NUMBER: " + orderNo);
		System.out.println("-------------------------- 报文明细_RESPONSE_CODE: " + responseCode);
		System.out.println("-------------------------- 报文明细_RESPONSE_DESC: " + responseDesc);
		System.out.println("-------------------------- 报文明细_PAYMENT_ID: " + paymentId);
		System.out.println("-------------------------- 报文明细_TRANS_DATE: " + transDate);
		System.out.println("-------------------------- 报文明细_TRANS_TIME: " + transTime);
		System.out.println("-------------------------- 报文明细_FEE_TYPE: " + feeType);
		System.out.println("-------------------------- 报文明细_PAY_WAY: " + payWay);
		System.out.println("-------------------------- 报文明细_BUSI_TYPE: " + busiType);
		System.out.println("-------------------------- 报文明细_BANK_ACCOUNT: " + bankAccount);
		System.out.println("-------------------------- 报文明细_BANK_CODE: " + bankCode);
		System.out.println("-------------------------- 报文明细_ACCOUNT_TYPE: " + accountType);
		System.out.println("-------------------------- 报文明细_HOLDER_NAME: " + holderName);
		System.out.println("-------------------------- 报文明细_HOLDER_CERTI_TYPE: " + holderCertiType);
		System.out.println("-------------------------- 报文明细_HOLDER_CERTI_CODE: " + holderCertiCode);
		System.out.println("-------------------------- 报文明细_TRANS_AMT: " + transAmt);
		System.out.println("-------------------------- 报文明细_CUR_TYPE: " + curType);
		System.out.println("-------------------------- 报文明细_RESERVED1: " + rsv1);
		System.out.println("-------------------------- 报文明细_RESERVED2: " + rsv2);
		System.out.println("-------------------------- 报文明细_ERR_MSG: " + errMsg);
		System.out.println("-------------------------- 报文明细_ERR_CODE: " + errCode);
	}

	public static Element ElementRoot(String xml) throws JDOMException, IOException {
		SAXBuilder saxBuilder = new SAXBuilder();
		StringReader stringIn = new StringReader(xml);
		Document document = saxBuilder.build(stringIn);

		return document.getRootElement();

	}

	private static String getElementValue(Element elmt) {
		return elmt == null ? "" : elmt.getValue();
	}
}
